import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Imitate {
    public static void main(String[] args) {
        String s = "aaa";
        char[] charArray = s.toCharArray();
        System.out.println(Arrays.toString(charArray));
//        s.replaceAll()
        StringBuilder stringBuilder = new StringBuilder(s);
        StringBuffer stringBuffer = new StringBuffer(stringBuilder);
        String.valueOf(charArray);
        List<List<Character>> list = new LinkedList<>();
        list.toString();
        System.out.println();
    }


    /**
     * 数青蛙
     */
    public int minNumberOfFrogs(String croakOfFrogs) {
        // c r o a k
        // 0 1 2 3 4
        int n = croakOfFrogs.length();
        if (n % 5 != 0) return -1;
        char[] cs = croakOfFrogs.toCharArray();
        int[] hash1 = new int[126];
        hash1['r'] = 'c';
        hash1['o'] = 'r';
        hash1['a'] = 'o';
        hash1['k'] = 'a';
        int[] hash = new int[126];
        for(char c : cs){
            if(c == 'c'){
                hash[c]++;
                hash['k'] = hash['k'] > 0 ? hash['k'] - 1 : hash['k'];
            }else if(hash[hash1[c]] > 0){
                hash[hash1[c]]--;
                hash[c]++;
            }else{
                return -1;
            }
        }
        if(hash['c'] !=0 || hash['r'] != 0 || hash['o'] != 0 || hash['a'] != 0) return -1;
        return hash['k'];
    }
    /**
     * 外观数列
     */
    public String countAndSay(int n) {
        if(n == 1){
            return "1";
        }
        String str = countAndSay(n-1);
        char[] cs = str.toCharArray();
        StringBuilder ret = new StringBuilder();
        int len = cs.length;
        for(int i = 0; i < len; i++){
            int count = 1;
            while(i + 1 < len && cs[i] == cs[i+1]){
                count++;
                i++;
            }
            ret.append(count);
            ret.append(cs[i]);
        }
        return ret.toString();
    }

    /**
     * Z 字形变换
     */
    public String convertBe(String s, int numRows) {
        int n = s.length();
        if (n == 1)
            return s;
        int index = 0;
        char[] cs = s.toCharArray();
        StringBuilder ret = new StringBuilder();
        int d = 2 * numRows - 2;
        if (d == 0)
            return s;
        for (int i = 0; i < n; i += d) {
            ret.append(cs[i]);
        }
        for (int i = 1; i < numRows - 1 && i < n; i++) {
            ret.append(cs[i]);
            for (int j = 1;; j++) {
                int index1 = j * d - i;
                if (index1 >= n)
                    break;
                ret.append(cs[index1]);
                int index2 = j * d + i;
                if (index2 >= n)
                    break;
                ret.append(cs[index2]);
            }
        }
        for (int i = numRows - 1; i < n; i += d) {
            ret.append(cs[i]);
        }
        return ret.toString();
    }
    public String convert(String s, int numRows) {
        int n = s.length();
        char[] cs = s.toCharArray();
        List<List<Character>> list = new LinkedList<>();
        for (int i = 0; i < numRows; i++) {
            list.add(new LinkedList());
        }
        for (int i = 0; i < n;) {
            //向下
            for (int j = 0; j < numRows && i < n; j++) {
                list.get(j).add(cs[i++]);
            }
            //像上
            for (int j = numRows - 2; j > 0 && i < n; j--) {
                list.get(j).add(cs[i++]);
            }
        }

        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            for (char c : list.get(i)) {
                ret.append(c);
            }
        }
        return ret.toString();
    }
    /**
     * 提莫攻击
     */
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        int ret = 0;
        int n = timeSeries.length;
        for(int i = 1; i < n; i++){
            int t = timeSeries[i] - timeSeries[i-1];
            if(t >= duration){
                ret+=duration;
            }else{
                ret+=t;
            }
        }
        return ret+duration;
    }
    /**
     * 替换所有的问号
     */
    public String modifyString(String s) {
        char[] chars = s.toCharArray();
        int n = s.length();
        for (int i = 0; i < n; i++) {
            if (chars[i] == '?') {
                for (char j = 'a'; j <= 'z'; j++) {
                    if((i == 0 || chars[i - 1] != j) && (i + 1 == n || chars[i+1] != j)){
                        chars[i] = j;
                        break;
                    }
                }
            }
        }
        // StringBuilder ret = new StringBuilder();
        // for (char i : chars)
        // ret.append(i);
        // return String.Va;
        return String.valueOf(chars);
    }
}
